package com.neusoft.bigdata.hbase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;

import com.neusoft.bigdata.hbase.util.CommonUtil;

/**
 * 用于数据删除
 * 
 * @author lihzh
 * @date 2013-4-3 上午9:19:01
 */
public class ForDelData {

	public static final String TABLE_NAME_METRIC = "metric";
	public static final String TABLE_NAME_METRIC_INDEX = "metric_time_index";
	public static final String TABLE_NAME_NEXT_TIME = "next_start_time";

	private static Configuration conf = null;
	private static HTable METRIC_HTABLE;
	private static final long DEL_TIME_POINT = CommonUtil.getTime(2011, 10, 28,
			0, 0);
	private static final long DEL_END_TIME_POINT = CommonUtil.getTime(2011, 10,
			29, 0, 0);
	private static final long TIME_INTERVAL = 60 * 1000L;

	static {
		Configuration HBASE_CONFIG = new Configuration();
		conf = HBaseConfiguration.create(HBASE_CONFIG);
		try {
			METRIC_HTABLE = new HTable(conf, TABLE_NAME_METRIC);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 * @author lihzh
	 * @throws IOException
	 * @date 2013-4-3 上午9:19:01
	 */
	public static void main(String[] args) throws IOException {
		List<Delete> deleteList = new ArrayList<Delete>();
		int count = 0;
		for (long t = DEL_TIME_POINT; t < DEL_END_TIME_POINT; t += TIME_INTERVAL) {
			for (int i = 1; i < ForPrepareData.RESOURCE_COUNT_ONE; i++) {
				String id = ForPrepareData.ID_PREFIX + CommonUtil.createId(i);
				String rowKey = id + "_" + t;
				Delete delete = new Delete(Bytes.toBytes(rowKey));
				deleteList.add(delete);
				count++;
				if (count == 1000) {
					METRIC_HTABLE.delete(deleteList);
					METRIC_HTABLE.flushCommits();
					deleteList.clear();
					count = 0;
				}
			}
		}
		METRIC_HTABLE.delete(deleteList);
		METRIC_HTABLE.flushCommits();
		System.out.println("Detele data finished.");
	}

}
